<?php
/**
* Users Management
*/
class Admin_UsersController extends Zend_Controller_Action{
		
	function init(){
		// System_Template::using('blank','Admin');
		System_Template::using('getit','Admin');
		
		// Lấy các parameter trong request URL
		$this->Params = $this->getRequest()->getParams();            
		$this->view->maintitle = "User Management";
		$this->model = new Admin_Model_Users();
		$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/log.txt');
		$this->logger = new Zend_Log($writer);
		parent::init();
	}
	
	/**
    * List Users
    */
	function indexAction(){           
		// Truyền giá trị search gồm : 1 chuỗi để search theo Tên + Email, giá trị RoleID và Status	
		$searchString = $this->_request->getParam("searchString");
		$searchEmail = $this->_request->getParam("searchEmail");
		$searchRole = $this->_request->getParam("searchRole");	
		$searchStatus = $this->_request->getParam("searchStatus");
		
		$parameter = array('searchString'=>$searchString,'searchEmail'=>$searchEmail,'searchRole'=>$searchRole,'searchStatus'=>$searchStatus);
		if(isset($this->Params['limit'])){
			$limit = (int) $this->Params['limit'];
		} else {
			$limit = 10;
		}
		if(isset($this->Params['page'])){
			$page = (int) $this->Params['page'];
		} else {
			$page = 1;
		}
		$numberOfItem = $this->model->count($parameter); 
		$data = $this->model->SelectAll($parameter,$limit,($page-1)*$limit); 
		// Đoạn này đảm bảo phân trang đúng khi xóa
		if($page>1 && sizeof($data)==0) {
			$page= $page - 1; 
			$data = $this->model->SelectAll($parameter,$limit,($page-1)*$limit); 
		}
		$this->view->name = $searchString;		
		$this->view->email = $searchEmail;		
		$this->view->roles = $searchRole;
		$this->view->status = $searchStatus;
		
		$this->ROLEMODEL = new Admin_Model_Roles();
        $listRole = $this->ROLEMODEL ->selectAll();
        $this->view->listRole = $listRole;
		
		$this->view->data = $data;
		$this->view->limit = $limit;
		$this->view->page = $page;
		$this->view->pagination = common::pagination('/admin/users/index',$numberOfItem, 'right',$limit,$page);
	}

	/**
    * INVISIBLE
	* Redirect to add or edit form
    */
    function inputAction(){
        $this->ROLEMODEL = new Admin_Model_Roles();
        $listRole = $this->ROLEMODEL ->selectAll();
        
        $id= (isset($this->Params['id'])?$this->Params['id']:'');
        
        /* Trường hợp cập nhật */
        if($id>0){            
            $data=$this->model->find($id)->current();
            $this->view->data=$data;
            $this->view->id = $id;
        }
        $this->view->listRole = $listRole;
    }
    
	/**
    * INVISIBLE
	* Save a new User or edit an existed User
    */
	function saveAction(){
        if(!isset($this->Params['PASSWORD'])){
			$this->Params['PASSWORD'] = 'getit@user';
			if($this->Params['STATUS']=='on'){
				$this -> Params ['STATUS'] = '1';
			} else {
				$this -> Params ['STATUS'] = '0';
			}
			if($this->Params['USERID']==''){
				$this->model->addNewUser($this->Params);
			} else {
				$this->model->updateUser($this->Params);
			}
		} else {
			$this->model->updateUser($this->Params);
		}
		
		// Lưu lại phân trang trước
		if(!isset($this->Params['limit'])){
			$this->_redirect('/admin/users/index');
		} else {
			$this->_redirect('/admin/users/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
		}
    }
	
	/**
    * INVISIBLE
	* Change status of this User
    */
    function changestatusAction() {
        $db = $this-> model -> getDefaultAdapter();
        $itemID = $this->Params['itemID'];
		$status = $this->Params['status'];
        $data = array('STATUS' => (int)$status);
        $rs = $db->update("user",$data, "USERID = " . (int) $itemID);
        exit;
    }
	
	/**
    * INVISIBLE
	* Change status of selected Users
    */
	function changestatusmultiitemsAction(){
		$ids = $this -> Params['itemIDs'];
		$status = $this -> Params['status'];
		
        $db = $this -> model -> getDefaultAdapter();
        foreach($ids as $t){
            $data = array('STATUS' => (int)$status);
			$rs = $db->update("user",$data, "USERID = " . (int) $t);
        }
	}
	
	/**
    * INVISIBLE
	* Delete an User
    */
    function deleteAction() {
        $itemID = $this -> Params['id'];
		
		// Logging class initialization
        $db = $this -> model -> getDefaultAdapter();
        $db->delete("user","USERID=".(int)$itemID);
        
        // Lưu lại phân trang trước
		if(!isset($this->Params['limit'])){
			$this->_redirect('/admin/users/index');
		} else {
			$this->_redirect('/admin/users/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
		}
    }
	
	/**
    * INVISIBLE
	* Delete selected User
    */
	function deletemultiitemAction(){
		$ids = $this -> Params['ITEM'];
		$this->logger->info(sizeof($ids));
        $db = $this -> model -> getDefaultAdapter();
        foreach($ids as $t){
			
            $db->delete("user","USERID=".(int)$t);
        }
        
        // Lưu lại phân trang trước
		if(!isset($this->Params['limit'])){
			$this->_redirect('/admin/users/index');
		} else {
			$this->_redirect('/admin/users/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
		}
	}
	
	/**
    * INVISIBLE
	* Change password of User
    */
	function changepassAction(){
		$itemID = $this->Params['id'];
		$data = $this->model->find($itemID)->current();
		$this->view->id = $itemID;
		$this->view->username = $data['FIRSTNAME'].' '.$data['LASTNAME'];

	}
}